package com.wjw.java.io.buffered_;

import java.io.*;

/**
 * 使用BufferedInputStream和BufferedOutputStream：拷贝二进制文件
 * 还可以拷贝文本文件
 */
public class BufferedCopy {

    public static void main(String[] args){
        //String srcPath = "F:\\IO\\aaa.jpg";//原路径
        //String destPath = "F:\\IO\\aaa2.jpg";//拷贝的目标路径

        String srcPath = "F:\\IO\\test_buffered_copy.txt";//原路径
        String destPath = "F:\\IO\\test_buffered_copy2.txt";//拷贝的目标路径

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcPath));
            bos = new BufferedOutputStream(new FileOutputStream(destPath));
            byte [] buff = new byte[1024];//一次读取多少个字节
            int len;//长度
            while ((len = bis.read(buff)) != -1){
                bos.write(buff,0,len);
            }
            System.out.println("文件拷贝完成。。。");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
